import inspect

from interkeys import HTTP
from class_excel import Reader,Writer


def get_func(obj,method):
    """
    获取指定对象的指定行为函数
    :param obj: 对象名称
    :param method: 函数名称(字符串数据)
    :return:
    """
    try:
        func = getattr(obj,method)
    except Exception as e:
        print("关键字%s不存在"%method)
        func = None

    if func is None:
        return

    args = inspect.getfullargspec(func).__str__()
    args = args[args.find('=') + 1:args.find(', varargs')]
    args = eval(args)
    args.remove('self')

    return func, len(args)

def runcase(obj,line):
    print(line[3])
    if len(line[0]) > 0 or len(line[1]) > 0:
        return

    func, num = get_func(obj, line[3])
    if num == 0:
        func()
    elif num == 1:
        func(line[4])
    elif num == 2:
        func(line[4], line[5])
    elif num == 3:
        func(line[4], line[5],line[6])
    else:
        print("暂时不支持超过3个参数")




reader = Reader()
reader.open_excel('./HTTP接口用例.xlsx')
writer = Writer()
writer.copy_open('./HTTP接口用例.xlsx','./HTTP接口用例-result.xlsx')

http = HTTP(writer)

sheetnames = reader.get_sheets()
for sheetname in sheetnames:
    reader.set_sheet(sheetname)
    writer.set_sheet(sheetname)
    lines =reader.readline()
    # print(lines)
    for row in range(len(lines)):
        # print(lines[row])
        http.r = row
        # print(http.r)
        runcase(http, lines[row])

writer.save_close()








